package com.senlingspace.payment.core.refund;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 退款策略工厂
 *
 * @author lebronjames
 * @date 2024/07/27
 */
@Component
public class RefundStrategyFactory {

    private final Map<Integer, RefundStrategy> strategyMap;

    @Autowired
    public RefundStrategyFactory(List<RefundStrategy> strategies) {
        this.strategyMap = strategies.stream().collect(Collectors.toMap(this::getSourceType, Function.identity()));
    }

    public RefundStrategy getStrategy(int sourceType) {
        RefundStrategy strategy = strategyMap.get(sourceType);
        if (strategy == null) {
            throw new IllegalArgumentException("Unknown source type: " + sourceType);
        }
        return strategy;
    }

    private Integer getSourceType(RefundStrategy strategy) {
        // 根据策略类来确定对应的 source
        if (strategy instanceof ShopRefundStrategy) return 10;
        if (strategy instanceof DouyinRefundStrategy) return 20;
        if (strategy instanceof MeituanRefundStrategy) return 30;
        if (strategy instanceof MiniProgramRefundStrategy) return 40;
        return 0;
    }

}
